import { image } from '@kit.ImageKit'

export function isArray<T>(arr: any, pre: (t: T)=>boolean): arr is T[] {
  return Array.isArray(arr) && arr.every(item => isType(item, pre))
}

function isType<T>(a: any, predicate: (e: T)=>boolean): a is T {
  return predicate(a)
}

export function isPixelMap(a: any): a is image.PixelMap {
  const r = a as image.PixelMap
  return r.isEditable !== undefined &&
    r.isStrideAlignment !== undefined &&
    r.readPixelsToBuffer !== undefined
}

export function jsonFrom<T>(json: string): T {
  return JSON.parse(json) as T
}